﻿<template>
	<div class="floors-container">
		<el-dialog v-model="state.isShowDialog" :width="640" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <Edit /> </el-icon>
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
						<el-input v-model="state.ruleForm.vId" />
						<el-input v-model="state.ruleForm.pId" />
					</el-form-item>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="上层Id" prop="parentId">
							<el-select clearable filterable v-model="state.ruleForm.parentId" placeholder="请选择上层Id">
								<el-option v-for="(item,index) in floorsParentIdDropdownList" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="小区Id" prop="villageId">
							<el-select clearable filterable v-model="state.ruleForm.villageId" placeholder="请选择小区Id">
								<el-option v-for="(item,index) in villageList" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="层级名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入层级名称" maxlength="64" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="层级编号" prop="code">
							<el-input v-model="state.ruleForm.code" placeholder="请输入层级编号" maxlength="64" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="层级描述" prop="desc">
							<el-input v-model="state.ruleForm.desc" placeholder="请输入层级描述" maxlength="128" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="叶子层级" prop="isLeaf">
							<el-radio-group v-model="state.ruleForm.isLeaf" :disabled="state.chdcount>0?true:false">
								<el-radio :value="true">是</el-radio>
								<el-radio :value="false">否</el-radio>
							</el-radio-group>
						</el-form-item>
						<el-alert type="info" show-icon :closable="false">
							<p>只有叶子层级才能添加资源，且叶子层级不能再添加子层级</p>
						</el-alert>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">{{ state.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
.dialog-footer{
	align:center;
}
</style>
<script lang="ts" setup>
	import { ref, onMounted, reactive  } from "vue";
	//import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import {useResourceApi} from '/@/api/resource'
	import { Edit } from '@element-plus/icons-vue';
	// import { addFloors, updateFloors, detailFloors } from "/@/api/main/floors";
	// import { getFloorsParentIdDropdown } from '/@/api/main/floors';
	// import { getVillageVillageIdDropdown } from '/@/api/main/floors';
	// import { getAPI } from '/@/utils/axios-utils';
	// import { SysEnumApi } from '/@/api-services/api';

	// const getEnumIsEndNodeData = ref<any>([]);
	//父级传递来的参数
	var props = defineProps({
		title: {
			type: String,
			default: "",
		},
	});
	//父级传递来的函数，用于回调
	const emits = defineEmits(["handleQuery"]);
	const ruleFormRef = ref();
	// const isShowDialog = ref(false);
	// const ruleForm = ref<any>({});

	
	const state = reactive({
		isShowDialog: false,
		ruleForm: {} as FloorForm,
		type: '',
		title: '',
		submitTxt: '',
		chdcount: 0,
		//villageList:[] as Array<VillageDropdownItem>,
		//floorList:[] as Array<FloorDropdownItem>,
		//getEnumStatusData: [],
		// ownOrgData: [],
	});
	//自行添加其他规则
	const rules = ref<FormRules>({
		floorName: [{required: true, message: '请输入层级名称！', trigger: 'blur',},],
	});

	// 打开弹窗
	const openDialog = async (type:string, row: FloorForm, _chdcount:any) => {
		if(_chdcount){
			state.chdcount = _chdcount;
		}
		console.log('rowf:'+JSON.stringify(row)+'cc:'+state.chdcount);
		if (type === 'edit') {
			state.ruleForm = row;
			state.title = '编辑层级';
			state.submitTxt = '修 改';
		} else {
			state.ruleForm = row;
			state.title = '新增层级';
			state.submitTxt = '新 增';
			// 清空表单，此项需加表单验证才能使用
			// nextTick(() => {
			// 	posDialogFormRef.value.resetFields();
			// });
		}
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		// let rowData = JSON.parse(JSON.stringify(row));
		// if (rowData.id)
		// 	ruleForm.value = (await detailFloors(rowData.id)).data.result;
		// else
		// 	ruleForm.value = rowData;
		state.isShowDialog = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emits('handleQuery');
		state.isShowDialog = false;
	};

	// 取消
	const cancel = () => {
		state.isShowDialog = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
					await useResourceApi().addFloor(state.ruleForm).then(async (res:AjaxResult)=>{
						if (res.success == true) {
							ElMessage.success('新增成功');
							emits('handleQuery');
							closeDialog();
						}
						else {
							ElMessage.warning('新增失败：' + res.message);
						}
					});
				} else {
					await useResourceApi().editFloor(state.ruleForm).then(async (res:AjaxResult)=>{
						if (res.success == true) {
							ElMessage.success('更新成功');
							emits('handleQuery');
							closeDialog();
						}
						else {
							ElMessage.warning('更新失败：' + res.message);
						}
					});
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};

	// const LoadVillageList = async()=>{
	// 	await apiResourceApi().getVillageList().then(async (res:AjaxResult)=>{
	// 		villageList = res?.res.data;
	// 	});
	// };



	// 页面加载时
	onMounted(async () => {
		//console.log('pc:'+props.chdcount)
			//getEnumIsEndNodeData.value = (await getAPI(SysEnumApi).apiSysEnumEnumDataListGet('YesNoEnum')).data.result ?? [];
	});

	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




